package cn.zongtilei.forword;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TextServlet extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");//这个是浏览器到服务器的乱码问题
		resp.setContentType("text/html;charset=utf-8");//这个是服务器到浏览器
		ServletContext servletContext = getServletContext();
		Integer count = (Integer) servletContext.getAttribute("count");
		if (count == null) {
			count = 1;
		}else {
			count += 1;
		}
		PrintWriter writer = resp.getWriter();
		writer.println("这是第"+count+"访问");	//不借助jsp也可以在页面打印
		servletContext.setAttribute("count", count);
		System.out.println(count);
	} 

}
